package com.starblink.product.detail.ui

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.basic.apollo.ext.ApolloValSetExt
import com.starblink.android.basic.ext.commContext
import com.starblink.android.basic.ext.toBean
import com.starblink.android.basic.ext.toJson
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.basic.apollo.ext.optional
import com.starblink.android.basic.util.ImageUtils
import com.starblink.product.detail.postrecommend.data.PicPost
import com.starblink.rocketreserver.SimilarProductRecommendV3Query
import com.starblink.rocketreserver.fragment.ProductD
import com.starblink.rocketreserver.type.ProductCollectionStatusReq
import com.starblink.rocketreserver.type.SimilarProductRecommendParam
import kotlinx.coroutines.launch

class ProductRecommendListVM : BaseViewModel() {
    var resultList: MutableLiveData<ArrayList<Any>> = MutableLiveData()
    var page = 1
    var isLoading = false

    /**
     * 获取相关推荐列表
     */
    fun getRecommend2(
        productId: String,
        standardSpuId: String,
        postId: String?,
        mountedProducts: List<ProductCollectionStatusReq>?,
        showLoading: Boolean = false
    ) {
        isLoading = true
        viewModelScope.launch {
            val response = SkGqlClient.skQuery(
                SimilarProductRecommendV3Query(
                    similarProductRecommendParam = SimilarProductRecommendParam(
                        page,
                        productId = if (productId.isNotEmpty()) productId.optional() else Optional.absent(),
                        standardSpuIds = standardSpuId,
                        productIdList = if (mountedProducts?.isNotEmpty() == true) mountedProducts.optional() else Optional.absent(),
                        pageSize = 20,
                        postId = if (postId?.isNotEmpty() == true) postId.optional() else Optional.Absent,
                    ),
                ), showDefaultLoading = showLoading

            )

            isLoading = false
            if (response?.data != null && response.data!!.similarProductRecommendV3.productSimilarRecommendVOF != null) {
                val productSimilarProductRecommend = response.data!!.similarProductRecommendV3.productSimilarRecommendVOF
                val postList = productSimilarProductRecommend.oneScreenOneCardList
                var list = arrayListOf<Any>()

                if (page == 1) {
                    var product = productSimilarProductRecommend.product?.productD
                    if (product != null) {
                        if (resultList.value?.isNotEmpty() == true) {
                            val oldPro = resultList.value!![0]
                            if (oldPro is ProductD && oldPro.id == product.id && oldPro.collected != product.collected) {//防止列表刷新后更改收藏状态丢失情况
                            Log.e("ProductRecommendListVM", "----11====${list}")
                                ApolloValSetExt.setApolloVal<ProductD>(
                                    product,
                                    "collected",
                                    oldPro.collected
                                )
                            }
                            resultList.value!![0] = product
                        } else {
                            resultList.value = arrayListOf()
                            resultList.value!!.add(product)
                        }
                    }
                }
                if (postList?.isNotEmpty() == true) {
                    for (i in postList.indices) {
                        val e = postList[i]
                        e?.oneScreenOneCardVOF?.let {//1:内容 2：商品
                            if (it.type == 2) {
                                it.product?.productD?.let { it1 ->
                                    list.add(it1)
                                }
                                ImageUtils.preloadImage(
                                    commContext(),
                                    it.product?.productD?.mainImg
                                )
                            } else {//图文和视频内容
                                if (it.post?.postD != null) {
                                    if (it.post?.postD!!.contentType == "1") {
                                        list.add(it.post?.postD!!.toJson().toBean<PicPost>())
                                    } else {
                                        list.add(it.post?.postD!!)
                                    }

                                    ImageUtils.preloadImage(
                                        commContext(),
                                        it.post?.postD!!.mainPic
                                    )
                                } else {

                                }

                            }
                        }
                    }
                    page++
                    if (resultList.value == null) {
                        resultList.value = arrayListOf()
                    }
                    resultList.value?.addAll(list)
                    resultList.value = resultList.value
                }
            }
        }
    }
}